package com.gome.oa.ess;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;

/**
 *
 * @author: tang li
 * @date: 2016年8月21日
 * @desc: 
 *
 */
public class EssLogin {

	private String username;
	private String password;
	
	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public boolean login() throws Exception {
		String mainUrl = "http://ess.gome.inc/";
		Http.get(mainUrl);
		
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		list.add(new BasicNameValuePair("account.apAccount", username));
		list.add(new BasicNameValuePair("account.apAccountpwd", password));
		
		String checkUrl = "http://ess.gome.inc/login_checkAccountAndPwd.action";
		CloseableHttpResponse res = Http.post(checkUrl, list);
		
		String json = EntityUtils.toString(res.getEntity());
		
		Map<?, ?> map = JSON.parseObject(json, Map.class);
		if(map != null && map.get("account") == null) {
			throw new Exception(((Map<?, ?>)map.get("message")).get("msg").toString());
		}
		
		List<NameValuePair> disableList = new ArrayList<NameValuePair>();
		disableList.add(new BasicNameValuePair("account.apAccount", username));
		String disableUrl = "http://ess.gome.inc/login_disabbleSessiond.action";
		CloseableHttpResponse disableRes = Http.post(disableUrl, disableList);
		
		String loginUrl = "http://ess.gome.inc/login_loginSystem.action";
		CloseableHttpResponse loginRes = Http.post(loginUrl, list);
		
		return true;
	}
}
